package com.tyq.auth.oauth2;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

import javax.annotation.Resource;

/**
 * 自定义token配置类
 *
 * @author 谭永强
 * @date 2024-05-30
 */
@Configuration
public class MyTokenStoreConfig {

    /**
     * jwt秘钥
     */
    private final static String STRING_KEY = "yunling";
    @Resource
    private RedisConnectionFactory redisConnectionFactory;

    /**
     * 将redis作为token存储
     *
     * @return TokenStore
     */
    @Bean
    public TokenStore redisTokenStore() {
        return new RedisTokenStore(redisConnectionFactory);
    }

    /**
     * 配置使用jwt作为令牌生成器
     *
     * @return token转换器
     */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        //设置jwt秘钥
        jwtAccessTokenConverter.setSigningKey(STRING_KEY);
        return jwtAccessTokenConverter;
    }
}
